/**
 * Project hdmap
 * @author shengjialiang
 * @version v1.0
 */


#include "map_datarender/ILayer.h"

/**
 * ILayer implementation
 */

namespace DataRender
{
    ILayer::ILayer()
		: mpRefDataSet(NULL), mpRefViewer(NULL), mLayerType(DataManager::DT_NONE),
		mVisible(true), mVisibleDistance(2000), mRenderType("")
    {

    }

    ILayer::~ILayer()
    {

    }

    void ILayer::setViewer(osgViewer::Viewer* in_pRefViewer)
    {
        this->mpRefViewer = in_pRefViewer;
    }

    osgViewer::Viewer* ILayer::getViewer()
    {
        return this->mpRefViewer;
    }

    void ILayer::setDataSet(DataManager::IDataSet* in_pRefDataSet)
    {
        this->mpRefDataSet = in_pRefDataSet;
    }

    DataManager::IDataSet* ILayer::getDataSet()
    {
        return this->mpRefDataSet;
    }

    void ILayer::setVisible(const bool& in_visible)
    {
        this->mVisible = in_visible;
    }

    bool ILayer::getVibible()
    {
        return this->mVisible;
    }

	void ILayer::setVisibleDistance(const double& in_visibleDistance)
	{
		this->mVisibleDistance = in_visibleDistance;
	}

	double ILayer::getVisibleDistance()
	{
		return this->mVisibleDistance;
	}

	std::string ILayer::getRenderType()
	{
		return this->mRenderType;
	}

	void ILayer::setRenderType(const std::string& in_renderType)
	{
		this->mRenderType = in_renderType;
	}

    DataManager::DataType ILayer::getLayerType()
    {
        return this->mLayerType;

    }

	CLayerAddEvent::CLayerAddEvent()
	{
		this->Layer = NULL;
	}

	CLayerAddEvent::CLayerAddEvent(Common::EventType type)
		:Common::IEvent(type)
	{
		this->Layer = NULL;
	}

	CLayerAddEvent::~CLayerAddEvent()
	{

	}

    static CLayerManager* g_pLayerManager = new CLayerManager();

    CLayerManager::CLayerManager()
    {

    }

    CLayerManager::~CLayerManager()
    {

    }

    CLayerManager* CLayerManager::instance()
    {
        return g_pLayerManager;
    }

    void CLayerManager::releaseInstance()
    {
        if (NULL != g_pLayerManager)
        {
            delete g_pLayerManager;
            g_pLayerManager = NULL;
        }
    }
    void CLayerManager::unloadAllayers()
    {
        std::vector<std::string> keys = this->getKeys();
        for (int i = 0; i < keys.size(); ++i)
        {
            std::string key = keys.at(i);
            ILayer* pLayer =
                dynamic_cast<ILayer*>(this->get(key));
            if (pLayer)
            {
                pLayer->unload();
            }
        }
    }

    void CLayerManager::removeAllLayers()
    {
        this->unloadAllayers();
        //this->removeAllAndDestroy();
        std::vector<std::string> keys = this->getKeys();
        for (int i = 0; i < keys.size(); ++i)
        {
            std::string key = keys.at(i);
            ILayer* pLayer =
                dynamic_cast<ILayer*>(this->remove(key));
            if (pLayer)
            {
                //delete pLayer;
            }
        }
        this->removeAll();
    }

	bool CLayerManager::add(const std::string& in_key, Common::CObject* in_pRefObject)
	{
		bool r = Common::CObjectMap::add(in_key, in_pRefObject);
		if (r)
		{
			CLayerAddEvent layerAddEvent;
			layerAddEvent.Layer = dynamic_cast<ILayer*>(in_pRefObject);
			this->notifyHandlers(&layerAddEvent);
		}
		return r;
	}

    ILayer* CLayerManager::getLayer(const std::string& in_businessType, const DataManager::DataType& in_layerType)
    {
        std::vector<std::string> keys = this->getKeys();
        for (int i = 0; i < keys.size(); ++i)
        {
            std::string key = keys.at(i);
            ILayer* pLayer =
                dynamic_cast<ILayer*>(this->get(key));
            if (pLayer 
                && pLayer->getType() == in_businessType
                && pLayer->getLayerType() == in_layerType)
            {
                return pLayer;
            }
        }
        return NULL;
    }

    /*void CLayerManager::initial()
    {

    }

    void CLayerManager::unInitial()
    {

    }*/
}